//
//  NumberToString.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/21.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class NumberToString: NSObject {
    //根据进制将int转换成float型的string形式 radix = 1 10 100 1000
    class func intToFloatString(number : Int, radix : Int)->String{
        let shangValue = number / radix
        let modValue = number - shangValue * radix
        let returnStr = String(shangValue) + "." + String(modValue)
        return returnStr
    }
    
    //返回该数字是radix的几次方
    class func digit(number : Int,radix : Int)->Int{
        if number <= 0 {
            return -1
        }
        var r = 0
        var a = number
        while a != 0 {
            a = a / radix
            r = r + 1
        }
        return r - 1
    }
    
    //返回以10进制表示的数字相应位置的数字
    class func num(number : Int,idx : Int)->Int{
        var bits = digit(number: number, radix: 10)
        if bits != -1 {
            bits += 1
            //idx会从1开始
            if idx > bits {
                return -1
            } else {
                var id : Array<Int> = []
                var tmp = number
                while tmp != 0 {
                    let mod = tmp % 10
                    tmp = tmp / 10
                    id.append(mod)
                }
                return id[idx - 1]
            }
        }
        return -1
    }
    
}
